﻿using CMS.BaseEntity;
using ElectronicSelling.DataAccess;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace ElectronicSelling.BusinessLogic
{
    public class CartLogic
    {
        /// <summary>
        /// lấy cartSession về DataTable
        /// </summary>
        /// <param name="cartSession"></param>
        public static DataTable GetCartFromSession(DataTable cartSession)
        {            
            DataTable dt = new DataTable();
            dt.Columns.Add("Position");
            dt.Columns.Add("ProductId");
            dt.Columns.Add("Image");
            dt.Columns.Add("Name");
            dt.Columns.Add("OldPrice");
            dt.Columns.Add("CurrentPrice");
            dt.Columns.Add("Quantity");
            dt.Columns.Add("Total");
            dt.Columns.Add("SectionId");
            dt.Columns.Add("CategoryId");
            int pos = 0;
            foreach (DataRow dr in cartSession.Rows)
            {
                pos++;
                Product product = new Product();
                product.ProductId = int.Parse(dr["ProductId"].ToString());
                DataTable temp = HomeProductLogic.GetProductDetailById(product);
                DataRow row = temp.Rows[0];
                DataRow rowdt = dt.NewRow();
                rowdt["Position"] = pos;
                rowdt["ProductId"] = row["ProductId"];
                rowdt["Image"] = row["Image"];
                rowdt["Name"] = row["Name"];
                rowdt["OldPrice"] = String.Format("{0:0,0}",row["OldPrice"]);
                rowdt["CurrentPrice"] = String.Format("{0:0,0}",row["CurrentPrice"]);
                rowdt["Quantity"] = dr["Quantity"];
                rowdt["Total"] = double.Parse(row["CurrentPrice"].ToString()) * double.Parse(dr["Quantity"].ToString());
                rowdt["SectionId"] = row["SectionId"];
                rowdt["CategoryId"] = row["CategoryId"];
                dt.Rows.Add(rowdt);
            }
            return dt;
        }

        public static void CartUpdate(DataTable cartDetail, string proId, string inumber)
        {
            if (cartDetail.Rows.Count > 0)
            {
                for (int i = 0; i < cartDetail.Rows.Count; i++)
                {
                    if (cartDetail.Rows[i]["ProductId"].ToString().Equals(proId))
                    {
                        cartDetail.Rows[i]["Quantity"] = inumber;
                        break;
                    }
                }
            }
        }

        /// <summary>
        /// get product for cart
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        public static DataTable GetProductDetailById(Product product)
        {
            return HomeProductAccess.GetProductDetailById(product);
        }

        public static double GetTotalDouble(DataTable cartSession)
        {
            DataTable dt = new DataTable();
            dt = GetCartFromSession(cartSession);
            double sumTotal = 0;
            foreach (DataRow dr in dt.Rows)
            {
                sumTotal += double.Parse(dr["Total"].ToString());
            }
            return sumTotal;
        }
        
    }
}